//
//  JSBookReaderController.m
//  NiubilityProject
//
//  Created by Apple on 2018/5/23.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSBookReaderController.h"
#import "JSTextContentViewController.h"
#import "JSReaderPageViewController.h"
#import "DZMCoverController.h"
#import "TYAttributedLabel.h"
#import "JSReaderBook.h"
#import "YYFPSLabel.h"




@interface JSBookReaderController ()<DZMCoverControllerDelegate,
UIPageViewControllerDataSource,
UIPageViewControllerDelegate,
JSBookPageStyleDelegate,
JSBookContenDateSource,
JSBookContenDateDelegate,
readerPageControllerDelegate
>

@property (nonatomic, weak) JSReaderPageViewController * pageViewController;
@property (nonatomic,weak) DZMCoverController *coverVC;
@property (nonatomic, strong) JSTextContentViewController *curVC;

@end

@implementation JSBookReaderController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.PageStyle = JSReaderTransitionStylePageBook;
    [JSBookReaderManager sharedJSBookReaderManager].pageDelegate = self;
    [JSBookReaderManager sharedJSBookReaderManager].contentDataDelegate = self;
    [JSBookReaderManager sharedJSBookReaderManager].contentDataSource = self;
    // 1.获取阅读记录 JSReaderBodel last
    [[JSBookReaderManager sharedJSBookReaderManager] loadBookData:123456];
    [[JSBookReaderManager sharedJSBookReaderManager] registerRunLoopObserver];
    
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if (!objc_getAssociatedObject(self, _cmd)) {
        [self customDidInitialized];
        objc_setAssociatedObject(self, @selector(viewDidLayoutSubviews), @(YES), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
}
#pragma mark - PageControllerDelegate -
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerBeforeViewController:(UIViewController *)viewController{
    if ([[JSBookReaderManager sharedJSBookReaderManager] havePreChapter]) {
        
        JSTextContentViewController *readerVC = [[JSTextContentViewController alloc]init];
        readerVC.contenL.attributedText = [[JSBookReaderManager sharedJSBookReaderManager] loadUpPageData:nil].attString;
        return readerVC;
    }
    return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController{
    if ([[JSBookReaderManager sharedJSBookReaderManager] haveNextChapter]) {
        
        JSTextContentViewController *readerVC = [[JSTextContentViewController alloc]init];
        readerVC.contenL.attributedText = [[JSBookReaderManager sharedJSBookReaderManager] loadNextPageData:nil].attString;
        return readerVC;
    }
    return nil;
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
    if (completed) {
        [[JSBookReaderManager sharedJSBookReaderManager] changeFinish];
    }
    self.curVC = previousViewControllers.firstObject;

}
#pragma mark - DZMCoverControllerDelegate

// 切换结果
- (void)coverController:(DZMCoverController *)coverController currentController:(UIViewController *)currentController finish:(BOOL)isFinish{
    if (isFinish) { // 切换失败
        NSLog(@"DZMCoverControllerDelegate 切换完成");
        [[JSBookReaderManager sharedJSBookReaderManager] changeFinish];
    }
        self.curVC = (JSTextContentViewController *)currentController;
}

// 上一个控制器
- (UIViewController *)coverController:(DZMCoverController *)coverController getAboveControllerWithCurrentController:(UIViewController *)currentController{
    if ([[JSBookReaderManager sharedJSBookReaderManager] havePreChapter]) {
        
        JSTextContentViewController *readerVC = [[JSTextContentViewController alloc]init];
        readerVC.contenL.attributedText = [[JSBookReaderManager sharedJSBookReaderManager] loadUpPageData:nil].attString;
        return readerVC;
    }
    return nil;
}

// 下一个控制器
- (UIViewController *)coverController:(DZMCoverController *)coverController getBelowControllerWithCurrentController:(UIViewController *)currentController{
    if ([[JSBookReaderManager sharedJSBookReaderManager] haveNextChapter]) {
        
        JSTextContentViewController *readerVC = [[JSTextContentViewController alloc] init];
        NSAttributedString *str = [[JSBookReaderManager sharedJSBookReaderManager] loadNextPageData:nil].attString;
        readerVC.contenL.attributedText = str;
        return readerVC;
    }
    return nil;
}
#pragma mark - JSBookPageStyleDelegate -
-(void)resetingBookPageStyle:(NSInteger)style{
    if (style == self.PageStyle) {
        return;
    }
    self.PageStyle = style;
    [self.coverVC.view removeFromSuperview];
    [self.coverVC removeFromParentViewController];
    [self.pageViewController.view removeFromSuperview];
    [self.pageViewController removeFromParentViewController];
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.25 animations:^{
            [self didInitialized];
        }];
    });
}
-(CGSize)renderSize{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, [UIScreen mainScreen].bounds.size.height - 20);
}
/** 获取到整本书的信息 */
-(void)reciveBookData:(JSReaderBook *)data{
    JSLog(@"获取到整本书的信息 %@", data);
    [[JSAlertHelper sharedJSAlertHelper] hideAnimated:YES];
}
/** 获取到章节信息 */
-(void)reciveChapterData:(JSReaderChapter *)data isRenader:(BOOL)renader{
    [[JSAlertHelper sharedJSAlertHelper] hideAnimated:YES];
    JSLog(@"获取到章节信息 %@", data);
   
}
- (void)startRequestData{
    [[JSAlertHelper sharedJSAlertHelper] showLoadingText:@"正在获取章节信息" inView:self.view animation:YES];
}
/** 获取到指定页信息 */
-(void)recivePageData:(JSReaderPager *)data isRenader:(BOOL)renader{
    if (renader) {
        NSLog(@"渲染章节内容到界面");
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.curVC.contenL setAttributedText:data.attString];
        });
    }
}
//MARK:点击中间
- (void)tapCenterClick:(CGPoint)loca{
    NSLog(@"点击中间");
}

- (void)dealloc{
    NSLog(@"ViewControllerBook 释放了");

    [[JSBookReaderManager sharedJSBookReaderManager] cancelRunloopObserver];
}

















- (void)customDidInitialized{
    // 1.初始化界面
    UIPageViewController *pageController = nil;
    DZMCoverController   *pageCoverController = nil;
    switch (_PageStyle) {
        case JSReaderTransitionStylePageNone:{
            
            pageCoverController = [[DZMCoverController alloc] init];
            self.coverVC = pageCoverController;
            pageCoverController.delegate = self;
            [self.view addSubview:pageCoverController.view];
            self.coverVC.openAnimate = NO;
            
            [self addChildViewController:pageCoverController];
            break;
        }
        case JSReaderTransitionStylePageCur:{
            pageCoverController = [[DZMCoverController alloc] init];
            self.coverVC = pageCoverController;
            pageCoverController.delegate = self;
            [self.view addSubview:pageCoverController.view];
            [self addChildViewController:pageCoverController];
            break;
        }
        case JSReaderTransitionStylePageBook:{
            pageController = [[JSReaderPageViewController alloc] init];
            self.pageViewController = pageController;
            pageController.delegate = self;
            pageController.dataSource = self;
            pageController.view.frame = self.view.bounds;
            [self addChildViewController:pageController];
            [self.view addSubview:pageController.view];
            break;
        }
        case JSReaderTransitionStyleScroll:{
            pageController = [[JSReaderPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
            self.pageViewController = pageController;
            pageController.delegate = self;
            pageController.dataSource = self;
            pageController.view.frame = self.view.bounds;
            [self addChildViewController:pageController];
            [self.view addSubview:pageController.view];
            break;
        }
    }
    // 界面显示
    if (self.pageViewController) {
        self.pageViewController.readerDelegate = self;
        if (!self.curVC) {
            JSTextContentViewController *readerVC = [[JSTextContentViewController alloc]init];
            self.curVC = readerVC;
        }
        //
        [self.pageViewController setViewControllers:@[self.curVC]
                                          direction:UIPageViewControllerNavigationDirectionForward
                                           animated:NO
                                         completion:nil];
    }else if (self.coverVC){
        if (!self.curVC) {
            JSTextContentViewController *readerVC = [[JSTextContentViewController alloc]init];
            self.curVC = readerVC;
        }
        //
        [self.coverVC setController:self.curVC];
    }
    @weakify(self)
    [[JSBookReaderManager sharedJSBookReaderManager] loadFromBook:1 PageData:^(JSReaderPager *data) {
        JSLog(@"%@", [NSThread currentThread]);
        @strongify(self)

        dispatch_async(dispatch_get_main_queue(), ^{
            self.curVC.contenL.attributedText = data.attString;

            
        });
    }];
}
-(UIImage *)navigationBarBackgroundImage{
    return [UIImage qmui_imageWithColor:[UIColor clearColor]];
}

@end
